package com.dd.edu.rabbitmqboot.business;

import com.alibaba.fastjson.JSONObject;
import com.dd.edu.rabbitmqboot.model.Order;
import com.dd.edu.rabbitmqboot.model.OrderDB;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.Date;
import java.util.UUID;

/**
 * 订单控制器
 */
@Controller
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private AmqpTemplate rabbitTemplate;
    /**
     * 提交订单
     * @return
     */
    @RequestMapping("/submit")
    @ResponseBody
    public String submit(){
        String orderNo = UUID.randomUUID().toString();

        final Order order = new Order(orderNo, "测试商品",
                new BigDecimal(1000.00), new Date(), Order.STATUS_WAIT_PAY);
        OrderDB.orderMap.put(orderNo, order);

        rabbitTemplate.convertAndSend("ex.order.ttl", "order.pay.ttl", JSONObject.toJSON(order).toString());

        return "下单成功待支付：" + orderNo;
    }

    /**
     * 订单列表
     * @return
     */
    @RequestMapping("/list")
    @ResponseBody
    public Object[] list(){
        return OrderDB.orderMap.values().toArray();
    }
}
